Ext.define('FirstMate.controller.Main', {
	extend: 'Ext.app.Controller',
	init: function(){		
		
    	Ext.fly('appLoadingIndicator').destroy();
    	Ext.Viewport.setMasked({xtype:'loadmask',message:'Initializing Application'});
    	
      	FirstMate.app.weatherCodesStore = Ext.create('FirstMate.store.MarineWeatherCodes');
    	
    	FirstMate.app.tidesStore = Ext.create('FirstMate.store.Tides');    	
    	FirstMate.app.tideStationsStore = Ext.create('FirstMate.store.TideStations');    	

       	
       	FirstMate.app.defaultPosition = new google.maps.LatLng(0, 0);
    	//FirstMate.app.position = new google.maps.LatLng(0, 0);
    	
        if (Ext.device.Geolocation){
          	Ext.device.Geolocation.getCurrentPosition({
  	          success: function(position) {        	        	  
  	        	  FirstMate.app.position = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
  	        	  FirstMate.app.getController('Map').resetMap();
  	          },
  	          failure: function() {
  	        	  FirstMate.app.position = new google.maps.LatLng(0, 0);
  	        	  Ext.Msg.alert('Problem accessing geolocation.', 'Please make sure geolocation is on', Ext.emptyFn);
  	          }
          	}); 	
        }else{
        	FirstMate.app.geo = Ext.create('Ext.util.Geolocation', {
		    autoUpdate: false,
		    //updateInterval : 60000,
		    timeout: 30000,
		    //allowHighAccuracy: true,
		    listeners: {
		        locationupdate: FirstMate.app.getController('Map').onUpdateLocation,
		        locationerror:  FirstMate.app.getController('Map').onLocationError
		    	}
        	});
        };
       	
    	FirstMate.app.tideStationsStore.on({
    	    load  : function(){
				FirstMate.app.bounds = FirstMate.app.getController('Map').getBounds(FirstMate.app.Marker.getPosition());
				Ext.getStore('tidesStationsStore').filterByGeoBounds(FirstMate.app.bounds);
				FirstMate.app.getController('Settings').setAppDate();
				//console.log('STATION LOAD TRIGGERED');
				Ext.getStore('tidesStore').loadAll(FirstMate.app.date);
    	    }
    	});
       	FirstMate.app.tideStationsStore.load();
    	FirstMate.app.tidesMarkers = new Array();
		
		Ext.Viewport.add(Ext.create('FirstMate.view.Main'));
		Ext.Viewport.setMasked(false);
//		Ext.EventManager.addListener(window, 'onbeforeunload', function() {
//		    return 'You will not be able to return to main app, until the web cache expires (in an hour)';
//		});
		//window.on('onbeforeunload', function() {
		//    return 'You will not be able to return to main app, until the web cache expires (in an hour)';
		//});
		//Ext.EventManager.addListener(window, 'onbeforeunload', this.onBeforeUnload, this);
//		window.onbeforeunload = function(){
//			  
//			    return "Are you sure you want to leave?  someCondition does not equal someValue..."
//
//			}
		
	}	 
//,
//	onBeforeUnload : function(evt) {
//        return "message";
//}
});